//
//  ZQFCycleView.m
//  ZQFCycleViewDemo
//
//  Created by Work on 15/7/24.
//  Copyright (c) 2015年 zengqingfu. All rights reserved.
//

#define LABELHEIGHT 30

#import "LXCycleView.h"
#import "LXDevSettingModel.h"

@interface LXCycleView() <UIScrollViewDelegate>
@property (nonatomic, assign) BOOL inRedrawPlace;
@property (nonatomic, assign) id<LXCycleViewDelegate> delegate;
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIView *showV1;
@property (nonatomic, strong) UIView *showV2;
@property (nonatomic, strong) UIView *showV3;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UITapGestureRecognizer *tapGestureRecognizer;

@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger totalCount;
@property (nonatomic, strong) LXLineChartView *sugarTrendLine1;

@end

@implementation LXCycleView

- (instancetype)initWithFrame:(CGRect)frame delegate:(id<LXCycleViewDelegate>)delegate
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = delegate;
        [self configSubView];
    }
    return self;
}

- (void)reload
{
    
}

- (void)configSubView
{
    self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    _backView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self addSubview:_backView];
    [_backView addGestureRecognizer:self.tapGestureRecognizer];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    _scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    _scrollView.pagingEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.delegate = self;
    [self.backView addSubview:_scrollView];
    
    self.showV1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self.scrollView addSubview:_showV1];
    
    self.showV2 = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
    [self.scrollView addSubview:_showV2];
    
    self.showV3 = [[UIView alloc] initWithFrame:CGRectMake(self.frame.size.width * 2, 0, self.frame.size.width, self.frame.size.height)];
    [self.scrollView addSubview:_showV3];
    
    if ([_delegate respondsToSelector:@selector(cycleView:didDisplayTitleLabel:index:)]) {
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.frame.size.height - LABELHEIGHT, self.frame.size.width, LABELHEIGHT)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self.backView addSubview:_titleLabel];
        
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height - LABELHEIGHT * 2, self.frame.size.width, LABELHEIGHT)];
        _pageControl.userInteractionEnabled = NO;
        [self.backView addSubview:_pageControl];
    } else {
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, self.frame.size.height - LABELHEIGHT * 1, self.frame.size.width, LABELHEIGHT)];
        _pageControl.userInteractionEnabled = NO;
        [self.backView addSubview:_pageControl];
    }
}

- (void)tapAction:(id)sender
{
    if ([_delegate respondsToSelector:@selector(cycleView:didTouchImageView:titleLabel:index:)]) {
        [_delegate cycleView:self didTouchImageView:self.showV2 titleLabel:self.titleLabel index:_currentPage];
    }
}

- (BOOL)startPlay
{
    _totalCount = [_delegate countOfCycleView:self];
    self.pageControl.numberOfPages = _totalCount;
    return YES;
}

- (void)layoutSubviews
{
    [self reloadFrames];
    [self reset];
}

- (void)reloadFrames
{
    self.scrollView.contentSize = CGSizeMake(self.frame.size.width * 3, self.frame.size.height);
    self.showV1.frame = CGRectMake(self.frame.size.width * 0, 0, self.frame.size.width, self.frame.size.height);
    self.showV2.frame = CGRectMake(self.frame.size.width * 1, 0, self.frame.size.width, self.frame.size.height);
    self.showV3.frame = CGRectMake(self.frame.size.width * 2, 0, self.frame.size.width, self.frame.size.height);
    
    if ([_delegate respondsToSelector:@selector(cycleView:didDisplayTitleLabel:index:)]) {
        self.titleLabel.frame = CGRectMake(0, self.frame.size.height - LABELHEIGHT, self.frame.size.width, LABELHEIGHT);
        self.pageControl.frame = CGRectMake(0, self.frame.size.height - LABELHEIGHT * 2, self.frame.size.width, LABELHEIGHT);
    } else {
        self.pageControl.frame = CGRectMake(0, self.frame.size.height - LABELHEIGHT * 1, self.frame.size.width, LABELHEIGHT);
    }
    self.sugarTrendLine1.frame = self.bounds;
}

- (void)resetImages
{
    if (_totalCount <= 0) {
        return;
    }
    NSInteger index1 = (_currentPage - 1 + _totalCount) % _totalCount;
    NSInteger index2 = _currentPage;
    NSInteger index3 = (_currentPage + 1 + _totalCount) % _totalCount;
    
    if ([_delegate respondsToSelector:@selector(cycleView:willDisplayImageView:index:)]) {
        [_delegate cycleView:self willDisplayImageView:self.showV1 index:index1];
        [_delegate cycleView:self willDisplayImageView:self.showV2 index:index2];
        [_delegate cycleView:self willDisplayImageView:self.showV3 index:index3];
    }
}

- (void)resetLabel
{
    if([_delegate respondsToSelector:@selector(cycleView:didDisplayTitleLabel:index:)]){
        [_delegate cycleView:self didDisplayTitleLabel:self.titleLabel index:_currentPage];
    }
}

- (void)resetOffset
{
    CGPoint offset = CGPointMake(self.frame.size.width, 0);
    [self.scrollView setContentOffset:offset];
}

- (void)reset
{
    [self resetOffset];
    [self resetImages];
    [self resetLabel];
}

- (void)currentPageDown
{
    _currentPage = (_currentPage - 1 + _totalCount) % _totalCount;
    self.pageControl.currentPage = _currentPage;
    if (self.delegate && [self.delegate respondsToSelector:@selector(cycleView:currentIndex:)]) {
        [self.delegate cycleView:self currentIndex:_currentPage];
    }
}

- (void)currentPageUp
{
    _currentPage = (_currentPage + 1) % _totalCount;
    self.pageControl.currentPage = _currentPage;
    if (self.delegate && [self.delegate respondsToSelector:@selector(cycleView:currentIndex:)]) {
        [self.delegate cycleView:self currentIndex:_currentPage];
    }
}

- (void)nextPage
{
    CGPoint offset = CGPointMake(self.scrollView.frame.size.width * 2, 0);
    [self.scrollView setContentOffset:offset animated:YES];
}

- (void)goToPage:(NSInteger)index
{
    _currentPage = index;
    self.pageControl.currentPage = _currentPage;
    if (self.delegate && [self.delegate respondsToSelector:@selector(cycleView:currentIndex:)]) {
        [self.delegate cycleView:self currentIndex:_currentPage];
    }
    [self reset];
    [self performSelector:@selector(reDrawView) withObject:nil afterDelay:0.5];
}

- (void)refreshView:(NSMutableArray *)dataArray hasCut:(BOOL)cut
{
    if (cut) {
        NSDateFormatter *formate = [[NSDateFormatter alloc] init];
        [formate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *startDate = [formate dateFromString:self.startDate];
        NSMutableArray *dataX = [[NSMutableArray alloc] init];
        for (int i = 0; i < 7; i ++) {
            NSDate *newdate = [[NSDate date] initWithTimeInterval:(i * 60) sinceDate:startDate];
            [formate setDateFormat:@"HH:mm"];
            NSString *dateString = [formate stringFromDate:newdate];
            [dataX addObject:dateString];
        }
        self.sugarTrendLine1.dataArrOfX = dataX;
        [self reDrawView];
    }
    if (!self.inRedrawPlace) {
        LXFoodItem *item = [dataArray objectAtIndex:self.currentPage];
        [self.sugarTrendLine1 beginDrawView:item.tempNum];
    }
}

- (void)reDrawView {
    NSArray *array = [self.showDataDic valueForKey:[NSString stringWithFormat:@"%ld", (long)self.currentPage]];
    self.inRedrawPlace = YES;
    BOOL result = [self.sugarTrendLine1 reDrawView:array];
    self.inRedrawPlace = !result;
}

- (void)drawXandY
{
    self.sugarTrendLine1.frame = self.bounds;
    [self.sugarTrendLine1 drawXandY];
    [self reDrawView];
}
#pragma mark - strollView代理-
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint currentOffset = self.scrollView.contentOffset;
    if (currentOffset.x <= 0) {
        [self currentPageDown];
        [self reset];
    } else if (currentOffset.x >= self.scrollView.frame.size.width*2) {
        [self currentPageUp];
        [self reset];
    }
    [self reDrawView];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    
    CGPoint currentOffset = self.scrollView.contentOffset;
    CGFloat width = self.scrollView.frame.size.width;
    
    if (currentOffset.x > width && currentOffset.x < width * 2) {
        [self nextPage];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    
}

- (LXLineChartView *)sugarTrendLine1 {
    if (!_sugarTrendLine1) {
        NSDateFormatter *formate = [[NSDateFormatter alloc] init];
        [formate setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *startDate = [formate dateFromString:self.startDate];
        NSMutableArray *dataX = [[NSMutableArray alloc] init];
        for (int i = 0; i < 7; i ++) {
            NSDate *newdate = [[NSDate date] initWithTimeInterval:(i * 60) sinceDate:startDate];
            [formate setDateFormat:@"HH:mm"];
            NSString *dateString = [formate stringFromDate:newdate];
            [dataX addObject:dateString];
        }
        _sugarTrendLine1 = [[LXLineChartView alloc] initWithFrame:self.bounds];
        _sugarTrendLine1.backgroundColor = [UIColor whiteColor];
        _sugarTrendLine1.titleOfYStr = @"";
        _sugarTrendLine1.titleOfXStr = @"(minute)";
        NSMutableArray *array = [[NSMutableArray alloc] init];
        CGFloat persentNum = 1;
        NSInteger addNum = 0;
        if ([LXDevSettingModel shareSettingModel].tempUnitF) {
            persentNum = 1.8;
        }
        for (NSInteger i = 0; i < 7; i ++) {
            NSString *yString = [NSString stringWithFormat:@"%.0f", ((6 - i) * 50) * persentNum + addNum];
            [array addObject:yString];
        }
        _sugarTrendLine1.dataArrOfY = [NSArray arrayWithArray:array];
        _sugarTrendLine1.dataArrOfX = dataX;
        [self.showV2 addSubview:_sugarTrendLine1];
    }
    return _sugarTrendLine1;
}
@end
